<?

	class TarIndex extends json{
	
		var $base;
		var $options;
		var $index;
		var $files;
	
		function __construct($ids, $with_templates=false, $with_nicenames=false, $locality=DOM_ID){
			$this->options = array(
				'templates'=>$with_templates,
				'nicenames'=>$with_nicenames,
				'locality'=>$locality
			);
			$this->index = new json;			
			foreach($ids as $id){
				if(!$file = $this->db()->get($id)) continue;
				$path = $file->path().'/';
				if(!$this->base) $this->base = $path;
				// all passed ids must be children of the same directory
				elseif($path !== $this->base) continue;
				$file->join_index($this->index);
			}
			
			$tmp = realpath(Cache::mktemp());
			$this->options['tmp_dir'] = $tmp.'/files';
			mkdir($this->options['tmp_dir'], 0755);
			
			if($this->options['templates']){
				foreach($this->index as $id=>$href){
					$this->index_template_of($this->db()->get($id));
				}
			}
			
			$this->files = new json;
			
			foreach($this->index as $id=>$href){
				$path = '/'.substr($href, strlen($this->base)-1);
				$this->index->set($id, $path);
				$this->files->set($path, new json);
			}
		}
		
		function index_template_of($file){
			if(!$file) return;
			if((!$tmpl = $file->tmpl)||($this->index->get($tmpl))) return;
			if((!$template = $this->db()->get($tmpl))||(!$template->is_readable)) return;
			// so now there's at least one template 
			if(!$this->index->get('templates')) $this->index->set('templates', $this->base.$this->template_directory());
			$template->join_index($this->index);
			$tmpl_path = $this->index->templates.'/'.$template->subname()." ($tmpl)";
			if($xtn = $template->xtn()) $tmpl_path .= ".$xtn";
			$this->index->set($tmpl, $tmpl_path);
			$this->index_template_of($template);
		}
		
		function db(){
			return proxy($this->options['locality']);	
		}
		
		function copy(){
			$tmp = $this->options['tmp_dir'];
			foreach($this->index as $id=>$path){
				if($id == 'templates') continue;
				$dest = $tmp.$path;
				$file = $this->db()->get($id);
				$file->dir ? mkdir($dest, 0755) : copy($file->realpath(), $dest);
			}
			return $tmp;
		}
		
		function scrape(){
			foreach($this->index as $id=>$path){
				if($id == 'templates') continue;
				$file = $this->db()->get($id);
				$this->files->get($path)->set(
					'id', $file->id,
					'is_dir', is_dir($file->realpath()),
					'own', $file->own,
					'grp', $file->grp,
					'mod', $file->mode(),
					'title', $file->title,
					'tags', $file->tags,
					'description', $file->description,
					'meta', $file->meta,
					'body', $file->body,
					'tmpl', $file->tmpl
				);
			}
		}
		
		function pack(){
			$tmp_dir = $this->options['tmp_dir'];
			$tmp_base = dirname($tmp_dir);
			unset($this->options['tmp_dir']);
			$json = json_encode($this);
			$this->options['tmp_dir'] = $tmp_dir;
			$fh = fopen("$tmp_base/smuoi_tarindex", 'wb+');
			fwrite($fh, $json);
			fclose($fh);
			chdir($tmp_base);
			exec("tar -cvf Archive.tar files smuoi_tarindex");
			exec("gzip -vc Archive.tar > Archive.tgz");
			return "$tmp_base/Archive.tgz";
		}
		
		function template_directory(){
			if(!isset($this->options['template_directory'])){
				$td = filename_increment($this->options['tmp_dir'].'/templates');
				mkdir($td, 0755);
				$this->options['template_directory'] = basename($td);
			}
			return $this->options['template_directory'];
		}
	
		function cleanup(){
			$tmp = dirname($this->options['tmp_dir']);
			$cache_path = Cache::subpath($tmp, 1);
			Cache::clearCache($cache_path, 0, 1);
			@rmdir($tmp);	
		}
	}

?>